﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License") 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;

namespace NetworkAnalyzer.Engines.Graphics.Radar
{
    class RadarBuilder
    {
        public PointF RadarCenterPoint;
        public float Angle
        {
            get { return ((float)_i/_numberOfPoints)*360; }
        }

        private readonly float _radius;
        private readonly float _precision;
        private readonly List<PointF> RadarLinePoints = new List<PointF>();
        private int _i = 0;
        private int _numberOfPoints = 0;

        public RadarBuilder(PointF center, float radius, float precision)
        {
            RadarCenterPoint = center;
            _radius = radius;
            _precision = precision;

            Build();
        }

        private void Build()
        {
            for (float angle = 360; angle > 0; angle -= _precision)
            {
                RadarLinePoints.Add(
                    new PointF(
                        RadarCenterPoint.X + (float)Math.Sin(angle * Math.PI / 180) * _radius,
                        RadarCenterPoint.Y + (float)Math.Cos(angle * Math.PI / 180) * _radius
                    )
                );

                _numberOfPoints++;
            }
        }

        public PointF NextLinePoint()
        {
            _i = ++_i % _numberOfPoints;
            return RadarLinePoints.ElementAt(_i);
        }
    }
}
